package com.yaoandw.test;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import com.yaoandw.crawler.LinkQueue;

public class Mtest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Mtest test = new Mtest();
		test.index();
	}
	int poolSize = 3;
	ThreadPoolExecutor threadPool = new ThreadPoolExecutor(poolSize, poolSize, 3, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(poolSize),new ThreadPoolExecutor.CallerRunsPolicy()){
		@Override
		protected void afterExecute(Runnable r, Throwable t) {
			System.out.println(Thread.currentThread().getId()+" executed");
			super.afterExecute(r, t);
		}
	};
	public void index(){
		long t1 = System.currentTimeMillis();
		LinkQueue.addUnvisitedUrl("a");
		LinkQueue.addUnvisitedUrl("b");
		threadPool.execute(new Mtask(this));
		threadPool.execute(new Mtask(this));
		System.out.println(threadPool.getActiveCount());
		System.out.println(threadPool.getCompletedTaskCount());
		while(true){
			System.out.println(threadPool.getActiveCount());
			if(threadPool.getActiveCount() <= 0){
				threadPool.shutdown();
				break;
			}
			try {
//				synchronized (this) {
//					wait();
//				}
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println("total time used : "+(System.currentTimeMillis()-t1));
	}
	public void assign() {
		threadPool.execute(new Mtask(this));
		
	}
}
